package com.zmovie.admin.controller.movie;

import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.MovieAge;
import com.zmovie.framework.service.MovieAgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 电影年代模块
 * @since 2023-11-30 16:52
 */
@Controller
@RequestMapping("/movie/age")
@Api(tags = "电影年代模块")
public class MovieAgeController {
    @Autowired
    private MovieAgeService movieAgeService;

    @GetMapping
    @ApiOperation("电影年代首页")
    public String index(Model model){
        List<MovieAge> movieAges = movieAgeService.listMovieAge(new MovieAge());
        model.addAttribute(Constants.MOVIE_AGES_KEY, movieAges);
        return "movie/movie_age";
    }

    @GetMapping("/{id}")
    @ResponseBody
    @ApiOperation("根据id获取电影年代")
    public Object getMovieAge(@PathVariable("id") Integer id){
        return CommonReturnType.success(movieAgeService.getById(id));
    }

    @PostMapping
    @ResponseBody
    @ApiOperation("添加电影年代")
    public Object addMovieAge(@Validated MovieAge movieAge) throws BusinessException {
        try {
            boolean save = movieAgeService.save(movieAge);
            if(!save){
                return CommonReturnType.fail("添加失败!");
            }
        } catch (Exception e) {
            //SQL 名称重复
            throw new BusinessException(EnumBusinessError.NAME_EXISTS);
        }
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ResponseBody
    @ApiOperation("修改电影年代")
    public Object updateMovieAge(@Validated MovieAge movieAge) throws BusinessException {
        //参数校验
        if(movieAge.getId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieAgeService.updateById(movieAge);
        return CommonReturnType.success("修改成功!");
    }


    @DeleteMapping("/{id}")
    @ResponseBody
    @ApiOperation("删除电影年代")
    public Object deleteMovieAge(@PathVariable("id") @NotNull Integer id){
        movieAgeService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ResponseBody
    @ApiOperation("批量删除电影年代")
    public Object deleteMovieAgeBatch(Integer[] id) throws BusinessException {
        //参数校验
        if(id == null || id.length == 0){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieAgeService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索电影年代")
    public Object search(MovieAge movieAge, Model model){
        List<MovieAge> movieAges = movieAgeService.listMovieAge(movieAge);
        model.addAttribute(Constants.MOVIE_AGES_KEY, movieAges);
        return "movie/movie_age :: ageList";
    }
}
